//
//  SYBaiBanColorViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/12/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYBaiBanColorViewController.h"
#import "SYBaiBanColorModel.h"
#import "SYBaiBanColorCollectionViewCell.h"

@interface SYBaiBanColorViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *curCollectionView;
@property (strong, nonatomic) SYBaiBanColorModel *colorModel;
@property (strong, nonatomic) NSMutableArray *colorNameArr;//色值
@property (assign, nonatomic) NSInteger index;
@end

@implementation SYBaiBanColorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat w = 60;
    CGFloat h = 40;
    flowLayout.itemSize = CGSizeMake(w , h );
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.curCollectionView setCollectionViewLayout:flowLayout];
    self.curCollectionView.backgroundColor = [UIColor whiteColor];
    self.curCollectionView.delegate = self;
    self.curCollectionView.dataSource = self;
    
    self.colorNameArr = [SYBaiBanColorModel colorNameArr];
    [_curCollectionView registerNib:[UINib nibWithNibName:@"SYBaiBanColorCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SYBaiBanColorCollectionViewCell"];
}
#pragma mark - dele
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.colorNameArr.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SYBaiBanColorCollectionViewCell *colorCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SYBaiBanColorCollectionViewCell" forIndexPath:indexPath];
    SYBaiBanColorModel *colorModel = self.colorNameArr[indexPath.row];
    if (colorModel.isSelect == YES) {
        self.index = indexPath.row;
        colorCell.colorView.layer.borderWidth = 2;
        colorCell.colorView.layer.borderColor = [UIColor colorWithHexString:@"#20b097"].CGColor;
    } else {
        colorCell.colorView.layer.borderWidth = 0;
    }
    colorCell.colorView.backgroundColor = [UIColor colorWithHexString:colorModel.color];
    return colorCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SYBaiBanColorModel *selectColorModel = self.colorNameArr[_index];
    selectColorModel.isSelect = NO;
    SYBaiBanColorModel *colorModel = self.colorNameArr[indexPath.row];
    colorModel.isSelect = YES;
    
    
    if ([self.delegate respondsToSelector:@selector(selectItemColorHex:)]) {
        [self.delegate selectItemColorHex:colorModel.color];
    }
    [self.curCollectionView reloadData];
}

@end
